package me.itsoo.artemis.framework.ioc.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import me.itsoo.artemis.framework.core.util.AnnotationUtils;
import me.itsoo.artemis.framework.core.util.Assert;
import me.itsoo.artemis.framework.core.util.StringUtils;
import me.itsoo.artemis.framework.ioc.lang.Bean;

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * BeanUtils
 *
 * @author zxy
 * @version 0.0.1
 * @since 2023/6/6
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BeanUtils {

    /**
     * 给定的类是否为 Bean
     *
     * @param beanClass Class
     * @return boolean
     */
    public static boolean isBean(Class<?> beanClass) {
        final Bean ann = AnnotationUtils.findAnnotation(beanClass, Bean.class);
        return ann != null;
    }

    /**
     * 获取 Bean 名称
     *
     * @param beanClass Class
     * @return String
     */
    public static String getBeanName(Class<?> beanClass) {
        final Bean ann = AnnotationUtils.findAnnotation(beanClass, Bean.class);
        Assert.notNull(ann, beanClass.getName() + ": must be with @Bean");
        return getBeanName(ann.value(), beanClass);
    }

    /**
     * 获取 Bean 名称
     *
     * @param beanName  String
     * @param beanClass Class
     * @return String
     */
    public static String getBeanName(String beanName, Class<?> beanClass) {
        final String simpleName = StringUtils.lowerFirstLetter(beanClass.getSimpleName());
        return StringUtils.defaultIfBlank(beanName, simpleName);
    }

    /**
     * 获取 Bean 别名（去重）
     *
     * @param aliasName String[]
     * @return String[]
     */
    public static Set<String> getAliasNames(String... aliasName) {
        final List<String> aliasNames = Arrays.asList(aliasName);
        return new LinkedHashSet<>(aliasNames);
    }
}
